﻿using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Deploy.AuthorizationReq
{
    ///// <summary>
    ///// 管理员权限
    ///// </summary>
    //[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    //public class AdminAttribute : AuthorizeAttribute
    //{
    //    public AdminAttribute() : base("AdminPolicy") {}
    //}
    //public class AdminExtendRequirement : IAuthorizationRequirement
    //{
    //}
    //public class AdminExtendRequirementHandler : AuthorizationHandler<AdminExtendRequirement>
    //{
    //    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AdminExtendRequirement requirement)
    //    {
    //        #region 处理验证逻辑
    //        bool tokenExists = false;

    //        if (context.User != null && context.User.HasClaim(c => c.Type == "IsAdmin"))
    //        {
    //            var admin = context.User.FindFirst(c => c.Type == "IsAdmin").Value;
    //            if (admin == "true" || admin == "True")
    //                tokenExists = true;
    //            else
    //                tokenExists = false;
    //        }
    //        else if (context.User.Claims.Count() == 0)
    //        {
    //            context.Fail();
    //            return Task.CompletedTask;
    //        }

    //        if (tokenExists)
    //        {
    //            context.Succeed(requirement); // 显式的声明验证成功
    //        }
    //        else
    //        {
    //            context.Fail();// 显式的声明验证失败
    //        }
    //        #endregion
    //        return Task.CompletedTask;
    //    }
    //}
}
